package com.jn.dagger.database;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

import com.jn.dagger.entity.Course;

import java.util.ArrayList;
import java.util.List;

public class CourseDBHelper extends SQLiteOpenHelper {
    private static final String TAG = "CourseDBHelper" ;
    private static final String DATABASE_NAME = "db_course_manager" ;
    private static final int DATABASE_VERSION = 1 ;
    private static final String TABLE_NAME_COURSE = "course" ;
    public CourseDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        initTable(db);
        initData(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
    private void initTable(SQLiteDatabase db){
        db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME_COURSE + " (" +
                "_id integer primary key autoincrement ," +
                "course_name text ," +
                "course_time int ," +
                "teacher text ," +
                "classroom text)");
    }

    private void initData(SQLiteDatabase db){
        db.execSQL("insert into " + TABLE_NAME_COURSE + " (" +
                "course_name , course_time , teacher , classroom) " +
                "values " + "('安卓程序设计' , 60 , 'lidalong' , 'f606' )");
        db.execSQL("insert into " + TABLE_NAME_COURSE + " (" +
                "course_name , course_time , teacher , classroom) " +
                "values " + "('j2ee' , 48 , 'baibo' , 'f606' )");
        db.execSQL("insert into " + TABLE_NAME_COURSE + " (" +
                "course_name , course_time , teacher , classroom) " +
                "values " + "('web vue' , 60 , 'gaofeng' , 'f606' )");
        db.execSQL("insert into " + TABLE_NAME_COURSE + " (" +
                "course_name , course_time , teacher , classroom) " +
                "values " + "('安卓企业开发' , 60 , 'lidalong' , 'f606' )");
    }

    public void insertCourse(Course course) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();

        // 添加课程信息到 ContentValues 对象
        values.put("course_name", course.getCourse_name());
        values.put("course_time", course.getCourse_time());
        values.put("teacher", course.getTeacher());
        values.put("classroom", course.getClassroom());

        try {
            // 插入数据
            db.insert(TABLE_NAME_COURSE, null, values);
        } catch (Exception e) {
            // 处理插入过程中可能出现的异常
            Log.e("DatabaseHelper", "Error while inserting course", e);
        } finally {
            // 关闭数据库连接
            if (db != null) {
//                db.close();
            }
        }
    }

    @SuppressLint("Range")
    public List<Course> selectAllCourse(){
        List<Course> list = new ArrayList<>() ;
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query(TABLE_NAME_COURSE , null , null ,
                null ,null,null,null);
        if (cursor != null && cursor.getCount() > 0){
            while (cursor.moveToNext()){
                Course course = new Course();
                course.setCourse_id(cursor.getInt(cursor.getColumnIndex("_id")));
                course.setCourse_name(cursor.getString(cursor.getColumnIndex("course_name")));
                course.setCourse_time(cursor.getInt(cursor.getColumnIndex("course_time")));
                course.setTeacher(cursor.getString(cursor.getColumnIndex("teacher")));
                course.setClassroom(cursor.getString(cursor.getColumnIndex("classroom")));
                list.add(course);
            }
        }
        Log.d(TAG, "selectAllCourse: " + list.size());
        return list ;
    }
}
